package main

import (
	"common/config"
	"common/logs"
	"connector/app"
	"context"
	gameConfig "framework/game"
	"github.com/spf13/cobra"
	"go.uber.org/zap"
	"log"
	"os"
)

//var configFile = flag.String("game", "application.yml", "game file")

var rootCmd = &cobra.Command{
	Use:   "connector",
	Short: "connector 管理连接，session以及路由请求",
	Long:  `connector 管理连接，session以及路由请求`,
	Run: func(cmd *cobra.Command, args []string) {
	},
	PostRun: func(cmd *cobra.Command, args []string) {
	},
}

var (
	configFile    string
	gameConfigDir string
	serverId      string
)

//var configFile = flag.String("config", "application.yml", "config file")

func init() {
	rootCmd.Flags().StringVar(&configFile, "config", "application.yml", "app config yml file")
	rootCmd.Flags().StringVar(&gameConfigDir, "gameDir", "../config", "game config dir")
	rootCmd.Flags().StringVar(&serverId, "serverId", "", "app server id， required")
	_ = rootCmd.MarkFlagRequired("serverId")
}

func main() {

	//1 加载配置
	//flag.Parse()

	//加载配置
	//1.加载配置
	if err := rootCmd.Execute(); err != nil {
		log.Println(err)
		os.Exit(1)
	}
	//config.InitConfig(*configFile)     //读取全局配置文件
	//gameConfig.InitConfig("../config") //读取游戏配置文件
	config.InitConfig(configFile)        //读取全局配置文件 可以切换命令行的形式
	gameConfig.InitConfig(gameConfigDir) //读取游戏配置文件
	//2 加载zap日志
	logs.InitLogger()

	//3 启动golang监控
	go func() {
		//err := metrics.Serve(fmt.Sprintf("0.0.0.0:%d", game.Conf.MetricPort))
		//if err != nil {
		//	panic(err)
		//}
		zap.S().Info("app connector metrics run success")
	}()
	err := app.Run(context.Background(), serverId)
	if err != nil {
		panic(err)
		os.Exit(-1)
	}
	zap.S().Info("app connector up success")
}
